﻿using System;
using System.Data;
using System.Reflection;

namespace Core
{
    [Serializable]
    public abstract class DBO<T>
        where T : DBO<T>
    {
        public static Type Controller
        {
            get
            {
                return typeof(Controller<T>);
            }
        }

        #region Properties
        private string _SortBy;
        /// <summary>
        /// Gets or sets bool? value for SortBy
        /// </summary>
        [ColumnAttribute("SortBy", SqlDbType.NVarChar, 255, false, false, true)]
        public string SortBy
        {
            get
            {
                return _SortBy;
            }
            set
            {
                _SortBy = value;
            }
        }

        private SortType _SortType;
        /// <summary>
        /// Gets or sets bool? value for SortType
        /// </summary>
        [ColumnAttribute("SortType", SqlDbType.Bit, 1, false, false, true)]
        public SortType SortType
        {
            get
            {
                return _SortType;
            }
            set
            {
                _SortType = value;
            }
        }

        private Exception _LastException;
        public Exception LastException
        {
            get
            {
                return _LastException;
            }
            set
            {
                _LastException = value;
            }
        }
        #endregion

        #region Methods
        public int Insert()
        {
            T item = this as T;
            Exception ex = null;

            int result = Controller<T>.Insert(item, ref ex);
            this.LastException = ex;

            return result;
        }

        public bool Update()
        {
            T item = this as T;
            Exception ex = null;

            bool result = Controller<T>.Update(item, ref ex);
            this.LastException = ex;

            return result;
        }

        public bool Delete()
        {
            T item = this as T;
            Exception ex = null;

            bool result = Controller<T>.Delete(item, ref ex);
            this.LastException = ex;

            return result;
        }
        #endregion
    }
}